using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace System.Collections.ObjectModel
{
    public class ObservableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, INotifyCollectionChanged
    {
        new public void Add(TKey key, TValue value)
        {
            base.Add(key, value);
            OnCollectionChanged(NotifyCollectionChangedAction.Add, new KeyValuePair<TKey, TValue>(key, value));
        }
        new public void Remove(TKey key)
        {
            base.Remove(key);
            OnCollectionChanged(NotifyCollectionChangedAction.Reset, null);
        }
        new public void Clear()
        {
            base.Clear();
            OnCollectionChanged(NotifyCollectionChangedAction.Reset, null);
        }
        new public TValue this[TKey key]
        {
            get {
                return base[key]; 
            }
            set
            {
                base[key] = value;
                OnCollectionChanged(NotifyCollectionChangedAction.Reset, null);
            }
        }
        private void OnCollectionChanged(NotifyCollectionChangedAction action, object sender)
        {
            if (CollectionChanged != null)
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                    CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, sender))));
            }
        }
        public event NotifyCollectionChangedEventHandler CollectionChanged;
    }
}
